﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Web;

public class UrlRewrite : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        string url = context.Request.Url.ToString();
        string localUrl = "http://10.100.133.216/Studio/";
        string sUrl = url.Replace(localUrl, "~/");
        string urlWithoutParam = url;
        if (url.IndexOf("?") > 0) urlWithoutParam = url.Substring(0, url.IndexOf("?"));
        if (System.IO.File.Exists(context.Server.MapPath(urlWithoutParam.Replace(localUrl, "")))
            || System.IO.File.Exists(context.Server.MapPath(urlWithoutParam.Replace(localUrl, "Default.aspx"))))
        {
           
        }
        else
        {
            sUrl = "~/Default.aspx";
        }
        
        //sUrl = "~/WebService/Edit.aspx?pn=Hotel Search Dev";
        //context.Response.Write(sUrl);
        //context.Response.End();
        if (true)
        {
            string html = context.Server.MapPath("~/Default.html");
            Stream s = GetFileStream(html);
            if (s != null)
            {
                using (s)
                {
                    Stream2Stream(s, context.Response.OutputStream);
                    context.Response.End();
                }
            }
            StringWriter sw = new StringWriter();
            context.Server.Execute(sUrl, sw);
            string content = sw.ToString();
            context.Response.Flush();
            try
            {
                using (FileStream fs = new FileStream(html, FileMode.Create, FileAccess.Write, FileShare.Write))
                {
                    using (StreamWriter streamWriter = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312")))
                    {
                        streamWriter.Write(content);
                    }
                }
            }
            finally
            {
                //Response.End();   
            }
            context.Response.End();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public void Stream2Stream(Stream src, Stream dst)
    {
        byte[] buf = new byte[4096];
        while (true)
        {
            int c = src.Read(buf, 0, buf.Length);
            if (c == 0)
                return;
            dst.Write(buf, 0, c);
        }
    }
    public Stream GetFileStream(string filename)
    {
        try
        {
            DateTime dt = File.GetLastWriteTime(filename);
            TimeSpan ts = dt - DateTime.Now;
            if (ts.TotalHours > 1)
                return null;
            return new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
        }
        catch
        {
            return null;
        }
    }
}